package me.luger.project.fof.controller.api;

import me.luger.core.auth.cookie.util.CookieAuthUtil;
import me.luger.core.commom.util.LogUtil;
import me.luger.core.mvc.controller.BaseController;
import me.luger.core.mvc.entity.Result;
import me.luger.project.fof.entity.UserInfo;
import me.luger.project.fof.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * UserInfo controller
 */
@Controller
@RequestMapping("/api/fof/userInfo")
public class UserInfoApiController extends BaseController {

    @Autowired
    private UserInfoService userInfoService;

    @RequestMapping(value = "",name = "接口描述")
    @ResponseBody
    public Object demo(){
        try{

        }catch (Exception e){
            return Result.error();
        }
        return Result.success();
    }

    @RequestMapping(value = "/login",name = "FOF用户登陆接口")
    @ResponseBody
    public Object login(String userName,String password){
        UserInfo userInfo = null;
        try{
            userInfo = userInfoService.login(userName,password);
            if(null == userInfo){
                return Result.info(-1,"账号密码不匹配");
            }
            CookieAuthUtil.setAuthCookie(request,response,userInfo.getId()+"");
        }catch (Exception e){
            return Result.error();
        }
        return Result.success().addDateItem("userName",userInfo.getName()).addDateItem("id",userInfo.getId());
    }

    @RequestMapping(value = "/loginOut",name = "FOF用户登陆接口")
    @ResponseBody
    public Object loginOut(){
        try{
            CookieAuthUtil.clearAuthCookie(response);
        }catch (Exception e){
            return Result.error();
        }
        return Result.success();
    }

    @RequestMapping(value = "/searchByName",name = "用户搜索tips接口")
    @ResponseBody
    public Object searchByName(String userName){
        List<UserInfo> userInfoList = null;
        try{
            userInfoList = userInfoService.searchByName(userName);
            if(null == userInfoList || userInfoList.size() == 0){
                return Result.info(-1,"未查到用户");
            }
        }catch (Exception e){
            LogUtil.errorLog(e,"");
            return Result.error();
        }
        return Result.success().addDateItem("userList",userInfoList);
    }

    @RequestMapping(value = "/update",name = "用户更新自己信息的接口")
    @ResponseBody
    public Object update(String oldPassword,UserInfo userInfo){
        try{
            Integer uid = userInfo.getId();
            UserInfo oldUserInfo = userInfoService.fetchById(uid);
            if(null == oldUserInfo || !oldPassword.equals(oldUserInfo.getPassword())){
                return Result.error("旧密码错误",null);
            }
            int ret = userInfoService.updateIgnoreNull(userInfo);
            if(ret == 1){
                return Result.success("修改成功",null);
            }else{
                return Result.error("修改失败",null);
            }
        }catch (Exception e){
            LogUtil.errorLog(e,"");
            return Result.error();
        }
    }
}
